package com.hjw.weixun.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.hjw.weiuxn.util.DateJsonValueProcessor;

import com.alisoft.xplatform.asf.cache.IMemcachedCache;
import com.hjw.weixun.cache.MemCacheManage;
import com.hjw.weixun.domain.Message;
import com.hjw.weixun.domain.User;
import com.hjw.weixun.domain.Weixun;
import com.hjw.weixun.redis.RedisQueue;
import com.hjw.weixun.service.FollowerService;
import com.hjw.weixun.service.MessageService;
import com.hjw.weixun.service.WeixunService;
import com.hjw.weixun.service.impl.FollowerServiceImpl;
import com.hjw.weixun.service.impl.MessageServiceImpl;
import com.hjw.weixun.service.impl.WeixunServiceImpl;

public class HePageAction extends GenericAction {
	private static final Logger logger = LoggerFactory
			.getLogger(HePageAction.class);
	private static final long serialVersionUID = 7676108201604890250L;

	private String path;

	private FollowerService followerService;

	private MessageService messageService;

	private WeixunService weixunService;

	private List<Message> msgList;

	// 我创建的微讯
	private List<Weixun> createdWeixunList;

	// 感兴趣的列表
	private List<Weixun> interestedList;

	// 我关注的
	private List<Weixun> followingList;

	private User user;

	public HePageAction() {
		followerService = FollowerServiceImpl.getInstance();
		messageService = MessageServiceImpl.getInstance();
		weixunService = WeixunServiceImpl.getInstance();
	}

	/**
	 * 获取全部数据的方法
	 */
	public String getAllData() {

		User userObj = super.getCurrentUser();

		if (null == userObj) {
			return LOGIN;
		}

		msgList = new ArrayList<Message>();
		path = "message/general.jsp";

		// 获取当前用户的id
		Integer userid = userObj.getUserid();

		// 根据当前的用户 在队列
		long start = 0;
		long end = -1;

		createdWeixunList = weixunService.getCreatedWeixunList(userid);

		// 我关注列表
		followingList = weixunService.getFollowingList(userid);
		// 我感兴趣的列表
		interestedList = weixunService.getInterestedList(userid);

		// 队列初始长度
		Integer ql = 0;
		List<String> vals = RedisQueue.jedis.lrange(
				String.valueOf(userid), start, end);

		// 如果redis中有数据
		if (null != vals && !vals.isEmpty()) {

			ql = vals.size();

			IMemcachedCache cache = MemCacheManage.getInstance().getCache();

			for (int j = 0; j < ql; j++) {
				if (cache.containsKey(vals.get(j))) {
					Message vs = (Message) cache.get(vals.get(j));
					msgList.add(vs);
				}
			}
		}

		this.getRequest().getSession().setAttribute("ql", ql);

		return SUCCESS;

	}



	/**
	 * 轮询获取数据
	 * 
	 * @return
	 */
	public String getAjaxData() {

		User currentUser = super.getCurrentUser();
		if (null == currentUser) {
			return super.renderResult(false);
		}

		List<Message> msg = new ArrayList<Message>();

		Integer length = (Integer) this.getRequest().getSession().getAttribute("ql");
		
		List<String> vals = RedisQueue.jedis.lrange(String.valueOf(user.getUserid()), 0, -1);
				
		if (length == vals.size()) {
			return super.renderResult(false);
		} else if (vals.size() > length) {
			List<String> lists = null;
			List<String> vs = RedisQueue.jedis.lrange(
					String.valueOf(currentUser.getUserid()), 0, vals.size() - length
							- 1);
			for (int i = 0; i < vs.size(); i++) {
				lists = new ArrayList<String>();
				// 根据这个values 作为key 在memcached 找 key 对应的vaue
				IMemcachedCache cache = MemCacheManage.getInstance().getCache();
				if (cache.containsKey(vals.get(i))) {
					// 存在 找 value 值
					msg.add((Message) cache.get(vals.get(i)));
					// 返回到页面上去 json
				}
			}
		}
		this.getRequest().getSession().setAttribute("ql", vals.size());
		
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(Date.class,
				new DateJsonValueProcessor("MM月dd日     HH:mm"));
		config.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object source, String name, Object value) {
				if (name.equals("message")) { // 要过滤的 ，Map对象中的
					return true;
				} else {
					return false;
				}
			}
		});

		String jsonStr = JSONArray.fromObject(msg,config).toString();

		renderHtml("{\"success\" : \"true\", \"data\":" + jsonStr + "}");
		return null;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public List<Weixun> getCreatedWeixunList() {
		return createdWeixunList;
	}

	public void setCreatedWeixunList(List<Weixun> createdWeixunList) {
		this.createdWeixunList = createdWeixunList;
	}

	public List<Weixun> getFollowingList() {
		return followingList;
	}

	public void setFollowingList(List<Weixun> followingList) {
		this.followingList = followingList;
	}

	public List<Weixun> getInterestedList() {
		return interestedList;
	}

	public void setInterestedList(List<Weixun> interestedList) {
		this.interestedList = interestedList;
	}

	public List<Message> getMsgList() {
		return msgList;
	}

	public void setMsgList(List<Message> msgList) {
		this.msgList = msgList;
	}
	
	

}
